package cn.cvs.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SecurityInterceptor extends HandlerInterceptorAdapter {

    /**
     * This implementation always returns {@code true}.
     *
     * @param request
     * @param response
     * @param handler
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取Session中的变量，判断用户是否登录
        HttpSession session = request.getSession();
        Object o = session.getAttribute("userSession");
        if (o != null) {
            return true;
        } else {
            response.sendRedirect("/401");
            return false;
        }
    }

}
